//
//  ViewController.m
//  UI24_KVO_ NSNotificationCenter
//
//  Created by Scott on 16/1/14.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "ViewController.h"
#import "CellForExtend.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *collectView;

@property (nonatomic, retain) UIView *viewForRedline;

@property (nonatomic, retain) UIButton *buttonForExtension;

@property (nonatomic, retain) UICollectionView *collectExten;

@property (nonatomic, assign) BOOL isShow;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self createRedLine];
    
    [self createCollectView];
    
    [self createKVOAction];
    
    [self createButton];
}

#pragma mark - Section: README
/** 
 * 功能描述: 
 * 1. 点击界面右侧的Button之后, 展开一个collectionView, 长按任意一个cell之后, 所有的cell左上角出现删除按键.
 * 2. 再次长按任意一个cell之后, 所有的cell左上角删除按键消失.
 * 3. 点击右侧button, collectionView收缩时, 所有cell的左上角删除按键消失.
 */


#pragma mark - Section: SubViews
- (void)createRedLine {
    
    self.viewForRedline = [[UIView alloc] initWithFrame:CGRectMake(0, 50, WIDTH / 5, 2)];
    self.viewForRedline.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.viewForRedline];

}


#pragma mark - Section: CollectionView
- (void)createCollectView {
    
    // layout
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    flowLayout.itemSize = CGSizeMake(WIDTH, HEIGHT - 52);
    
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    // collection
    self.collectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50 + 2, WIDTH, HEIGHT - 50 - 2) collectionViewLayout:flowLayout];
    [self.view addSubview:self.collectView];
    
    
    self.collectView.dataSource = self;
    self.collectView.delegate = self;
    
    self.collectView.backgroundColor = [UIColor whiteColor];
    
    self.collectView.pagingEnabled = YES;
    
    // regis
    [self.collectView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"poolForCell"];
}



#pragma mark  * collection DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (collectionView == self.collectView) {
        return 5;
        
    } else {
        
        return 10;
    }
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (collectionView == self.collectView) {
        
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForCell" forIndexPath:indexPath];
        
        cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.f green:arc4random() % 256 / 255.f blue:arc4random() % 256 / 255.f alpha:1.f];
        
        return cell;
        
    } else {
        
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForCellExten" forIndexPath:indexPath];
        return cell;
    }
}



#pragma mark - Section: 右侧展开button
- (void)createButton {
    
    self.buttonForExtension = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.buttonForExtension.frame = CGRectMake(WIDTH - 50, 20, 40, 30);
    
    [self.view addSubview:self.buttonForExtension];

    [self.buttonForExtension setImage:[UIImage imageNamed:@"down"] forState:UIControlStateNormal];
    
    [self.buttonForExtension addTarget:self action:@selector(handleExten:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)handleExten:(UIButton *)button {
    
    button.selected = !button.selected;
    
    if (button.isSelected) {
        if (self.collectExten == nil) {
            
            // 弹出的collectView
            UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
            
            layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
            
            layout.itemSize = CGSizeMake(65, 40);
            
            
            self.collectExten = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, WIDTH, 0) collectionViewLayout:layout] ;
            
            [self.view addSubview:self.collectExten];
            
            self.collectExten.delegate = self;
            self.collectExten.dataSource = self;
            
            // 背景颜色
            self.collectExten.backgroundColor = [UIColor whiteColor];
            
            // 添加手势
            [self addLongPress];
    
            // regis
            [self.collectExten registerNib:[UINib nibWithNibName:@"CellForExtend" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"poolForCellExten"];
        }
        
        // 动画效果展开collectionView.
        [UIView animateWithDuration:0.25 animations:^{
            
            self.collectExten.frame = CGRectMake(0, 50, WIDTH, HEIGHT - 50);
            
            button.transform = CGAffineTransformMakeRotation(M_PI);
        }];

    }
    
    else {
        
        // 动画效果收起collecitonView.
        [UIView animateWithDuration:0.25 animations:^{
            self.collectExten.frame = CGRectMake(0, 50, WIDTH, 0);
            
            button.transform = CGAffineTransformMakeRotation(0);

            
        } completion:^(BOOL finished) {
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"hideDele" object:nil userInfo:nil];
            
        }];
        
    }
}


#pragma mark - Section: KVO(键值观察者)
/** 核心: 观察者 观察 某一个对象的属性变化. */
- (void)createKVOAction {
    
    // VC观察collection的contentOffset属性.
    [self.collectView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:@"ddd"];
}


/** 当被观察值属性发生变化的时候, 调用此方法. */
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    
    CGFloat x = [[change objectForKey:@"new"] CGPointValue].x;
    
    CGFloat redX = x * WIDTH / 5 / WIDTH;
    
    // 修改Redline位置.
    self.viewForRedline.transform = CGAffineTransformMakeTranslation(redX, 0);
    
}


#pragma mark - Section: 知识点1 通知中心

#pragma mark controller 添加长按手势
- (void)addLongPress {
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
    
    [self.collectExten addGestureRecognizer:longPress];
    
}

- (void)handleLongPress:(UILongPressGestureRecognizer *)longPress {
    
        switch (longPress.state) {
            case UIGestureRecognizerStateBegan:
            {
                self.isShow = !self.isShow;
                if (self.isShow) {
                    
                    // 发送通知, 显示cell上的删除Button.
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"showDele" object:nil userInfo:nil];

                } else {
                
                    // 发送通知, 隐藏cell上删除Button.
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"hideDele" object:nil];
                }
                break;
            }
                
            default:
                break;
        }
}


#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
